function getUrlPlain() {
  if (!localStorage.match_url2) localStorage.match_url2 = "";
  return localStorage.match_url2;
}
var __url__matches = null;
function getUrlMatches() {
  if (!__url__matches)
    __url__matches = expandMatches(getUrlPlain());
  return __url__matches;
}

function setUrlMatches(urls) {
  localStorage.match_url2 = urls;
  __url__matches = expandMatches(urls);
}

function getTrustedClsids() {
  if (!localStorage.trustedclsids) localStorage.trustedclsids = "";
  return localStorage.trustedclsids;
}

function setTrustedClsids(clsid) {
  localStorage.trustedclsids = clsid;
}

function updateToWildCharPattern()
{
  if (localStorage.match_url) {
    var current = getUrlPlain();
    var list = localStorage.match_url.split("\n");
    for (var i = 0; i < list.length; ++i) {
      if (list[i] != "")
        current = "r/" + list[i] + "\n" + current;
    }
    localStorage.removeItem("match_url");
    setUrlMatches(current)
  };
}

function convertUrlWildCharToRegex(wild)
{
  if (wild.substr(0, 2) == "r/")
    return new RegExp(wild.substr(2));
  if (wild == "<all_urls>")
    wild = "*://*/*";
  var schemePos = wild.indexOf("://");
  if (schemePos == -1) return null;
  var scheme = wild.substr(0, schemePos);
  if (scheme == "*") scheme = "https?";
  if (scheme != "http" && scheme != "https" && scheme != "https?")
    return null;

  var hostPos = wild.indexOf("/", schemePos + 3);
  if (hostPos == -1) return null;
  var host = wild.substring(schemePos + 3, hostPos);
  if (host == "*")
    host = "[^/]*";
  else if (host.substr(0, 2) == "*.")
    host = "[^/]*\." + host.substr(2);
  else if (host.indexOf("*") != -1 || host == "")
    return null;
  var left = wild.substring(hostPos + 1).replace(/([\.\\\/\?\{\}])/g, "\\$1").replace("*", ".*");
  var regex = "^" + scheme + ":\\/\\/" + host + "\\/" + left + "$";
  return new RegExp(regex);
}

function expandMatches(urls) {
  var list = urls.split('\n');
  var list_regex = [];
  for (var i = 0; i < list.length; ++i) {
    if (list[i] != "") {
      var regex = convertUrlWildCharToRegex(list[i]);
      if (regex != null) 
        list_regex.push(regex);
      else
        throw {index: i, value:list[i]};
    }
  }
  return list_regex;
}

function isUrlMatched(url) {
  var list = getUrlMatches();
  for (var i = 0; i < list.length; ++i) {
    if (list[i].test(url)) return true;
  }
  return false;
}

function isClsidTrusted(clsid) {
  return getTrustedClsids().indexOf(clsid) != -1;
}

function getResponse(request, tab) {
  if (request.command == "ActionList") {
    var response = new Object();
    response.replace = isUrlMatched(request.url);
    if (!response.replace) response.replace = isClsidTrusted(request.clsid);

    response.checkForm = (request.id != "") && response.replace;
    return response;
  }
}

function startListener() {
  updateToWildCharPattern();
  chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if (!sender.tab) {
      console.error("Illegal call");
    }
    var response = getResponse(request, sender.tab);
    sendResponse(response);
  });
}